package com.topwalk.datacollect.manager;

import com.topwalk.datacollect.factoty.BaseFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 这里对各个类型的Factory进行注册。现在有ServerFactory，用于生成各个serviceImpl。
 *
 * 如果有其他的Factory，请继承com.topwalk.datacollect.factoty.BaseFactory，并在com.topwalk.datacollect.enumI.FactoryEnum中添加枚举值。
 *
 * 若要添加ServiceImpl,请根据接口URL创建对应的ServiceImpl，并继承BaseServer<T>，并在com.topwalk.datacollect.enumI.ServerEnum添加对应的枚举值。
 * 比如现在调用的是video接口，则创建VideoServiceImpl，并继承BaseServer<T>，并在com.topwalk.datacollect.enumI.ServerEnum添加 VEDIO("video") 枚举值。
 * 其中T是期望的返回结果
 */
@Component
public class FactoryManager implements BeanPostProcessor {
    private Map<String, BaseFactory> factoryMap = new HashMap<>();


    public BaseFactory getFactory(String factoryCode){
        return factoryMap.get(factoryCode);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        BaseFactory baseFactory;
        if (bean instanceof BaseFactory) {
            baseFactory = (BaseFactory) bean;
            factoryMap.put(baseFactory.getCode(),baseFactory);
        }
        return bean;
    }



}
